﻿using System.Collections.Generic;
using System.Web;
using OPM.Class;
using OPM.Models;

namespace OPM
{
    // คลาสจัดการผู้ล็อกอินปัจจุบัน
    public static class U
    {
        private const string UserSessionKey = "LoginUser";
        public static Dictionary<string, User> LastestSessionUsers = new Dictionary<string, User>();

        // จากนั้นเก็บลง Session
        public static bool LoginKeepSession(User user)
        {
            // keep session personal
            HttpContext.Current.Session[UserSessionKey] = user;

            // keep session server
            if (user != null)
            {
                LastestSessionUsers[user.id] = user;
            }

            // return success
            return user != null;
        }

        // ดึงข้อมูล Session ล้อกอิน
        public static User CurrentUser
        {
            get
            {
                return HttpContext.Current.Session != null
                    ? HttpContext.Current.Session[UserSessionKey] as User
                    : null;
            }
        }

        // ดึง Token จาก Session ล็อกอิน
        public static string Token
        {
            get
            {
                if (CurrentUser == null || string.IsNullOrWhiteSpace(CurrentUser.Token))
                {
              
                    HttpContext.Current.Response.Write(Util.RenderTagMetaRedirect("/Account/Login", "Login", ""));
                    HttpContext.Current.Response.End();
                }
                return CurrentUser == null ? string.Empty : CurrentUser.Token;
            }
        }

        // ตรวจสอบ Session ว่าล็อกอิน (ไม่คำนึงถึง Expire บน Service)
        public static bool IsLogin
        {
            get { return CurrentUser != null; }
        }

        public static string officer_id
        {
            get { return CurrentUser == null ? string.Empty : CurrentUser.officer_id; }
        }

        public static int RowPerPage
        {
            get { return CurrentUser == null || CurrentUser.row_per_page == 0 ? 10 : CurrentUser.row_per_page; }
        }

        public static string org_id
        {
            get { return CurrentUser == null ? string.Empty : CurrentUser.org_id; }
        }

        public static string role_id
        {
            get { return CurrentUser == null ? string.Empty : CurrentUser.role_id; }
        }

        public static string default_language
        {
            get { return CurrentUser == null ? string.Empty : CurrentUser.default_language; }
        }

        public static bool IsThai
        {
            get { return true;/// return CurrentUser == null || CurrentUser.default_language != "TH" ? false : true; 
                              }

        }

        public static string is_thai
        {
            get
            {
                return enum_is_thai.ToString();
            }
        }
        public static LanguageOpmUi enum_is_thai
        {
            get
            {
                return CurrentUser == null ||
                    string.IsNullOrWhiteSpace(CurrentUser.default_language) ||
                    CurrentUser.default_language.ToUpper() != "TH" ? LanguageOpmUi.EN : LanguageOpmUi.TH;
            }
        }

        public enum LanguageOpmUi
        {
            EN,
            TH
        }
    }
}